<template>
    <div>
         <div class="headstyle">
            <el-row :gutter="20">
                <el-col :span="16">
                    <div class="grid-content bg-purple">
                    <span class="locsty"><i class="el-icon-location iconsty"></i>当前位置：员工注册数据>员工错误数据修复</span>
                    </div>
                </el-col>                      
            </el-row>
        </div>
        <div class="contentstyle">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="160px" class="demo-ruleForm">
              <el-form-item label="员工姓名" prop="name">
                  <el-input v-model="ruleForm.name" class="inputEdit"></el-input>
              </el-form-item>
              <el-form-item label="员工手机号码" prop="phone">
                  <el-input v-model="ruleForm.phone" class="inputEdit"></el-input>
              </el-form-item>
              <el-form-item label="员工身份证号码" prop="card">
                  <el-input v-model="ruleForm.card" class="inputEdit"></el-input>
              </el-form-item>                
              <el-form-item label="请输入密码" prop="st_psw">
                  <el-input v-model="ruleForm.st_psw" class="inputEdit" type="password" auto-complete="new-password"></el-input>
                  <el-tag style="color:red"> 如需修改请删除后重新输入，不作修改请勿操作</el-tag>
              </el-form-item>                       
              <el-form-item>                
                  <el-button type="primary btnsty" @click="submitForm('ruleForm')">确定</el-button>
                  <el-button type="primary btnsty" @click="goTo('/studentinfolist')">关闭</el-button>
              </el-form-item>    
            </el-form>
        </div>
    </div>
</template>
<script>
export default {
  data() {
    var validatephone = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入手机号码"));
      } else {
        if (this.id != undefined) {
          if (this.st_phone == value) {
            callback();
          } else {
            if (!/^1[3456789]\d{9}$/.test(value)) {
              callback(new Error("手机号码格式错误，请重新输入！"));
            } else {
              this.$axios
                .get("/AppLogin/" + value + "/V1")
                .then(json => {
                  if (json.data == 'no') {
                    callback();
                  } else {
                    callback(new Error("手机号码已存在，请更换！"));
                  }
                });
            }
          }
        }
      }
    };
    var validatecard = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入身份证号码"));
      } else {
        if (this.id != undefined) {
          if (this.st_card == value) {
            callback();
          } else {
            var reg = /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)/;
            if (!reg.test(value)) {
              callback(new Error("身份证号码格式错误，请重新输入！"));
            } else {
              this.$axios
                .get("/AppLogin/" + value + "/V2")
                .then(json => {
                  if (json.data == 'no') {
                    callback();
                  } else {
                    callback(new Error("身份证号码已存在，请更换！"));
                  }
                });
            }
          }
        }
      }
    };
    return {
      id: this.$route.params.id,
      st_phone: "",
      st_card: "",
      ruleForm: {
        name: "", //员工姓名
        phone: "", //员工手机号码
        card: "", //员工身份证号码
        st_psw: "" //密码
      },
      rules: {
        phone: [{ required: true, validator: validatephone, trigger: "blur" }],
        card: [{ required: true, validator: validatecard, trigger: "blur" }],
        st_psw: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 8, message: "密码长度在不能小于8个字符", trigger: "blur" }
        ]
      },
      isflag: false
    };
  },
  mounted() {  
    this.id = this.$route.params.id;   
    if (this.id != undefined) {
      this.$axios
        .get("/AppLogin/" + this.id + "/V5")
        .then(Jsondata => {
          this.st_card = Jsondata.data.St_Card;
          this.st_phone = Jsondata.data.St_Phone;
          this.ruleForm.name = Jsondata.data.St_Name;
          this.ruleForm.phone = Jsondata.data.St_Phone;
          this.ruleForm.card = Jsondata.data.St_Card;
          this.ruleForm.st_psw = Jsondata.data.St_pwd;
        })
        .catch(error => {});
    } else {
      this.$message({
        type: "info",
        message: "数据读取错误，请返回重试！"
      });
      this.$router.push("/studentinfolist");
    }
  },
  methods: {
    goTo(path) {
      this.$router.replace(path);
    },
    submitForm(formName) {
      if (this.id != undefined) {
        this.$confirm(
          "请确认所有需要更新的数据是否正确，此更新会牵扯到多个关联数据，请慎重！",
          "提示"
        ).then(() => {
          this.$refs[formName].validate(valid => {
            if (valid) {
              var list = {
                St_Name: this.ruleForm.name,
                St_Phone: this.ruleForm.phone,
                St_Card: this.ruleForm.card,
                St_pwd: this.ruleForm.st_psw
              };
              this.$axios
                .put("/AppLogin/" + this.id + "/V4", list)
                .then(Jsondata => {
                  if (Jsondata.data == 'ok') {
                    this.$message({
                      type: "success",
                      message: "更新成功！"
                    });
                  } else{
                    this.$message({
                      type: "error",
                      message: "更新失败，请重试或检查登录状态！"
                    });
                  }
                })
                .catch(error => {});
            } else {             
              return false;
            }
          });
        });
      }
    }
  }
};
</script>

<style scoped>
/*.loc-index {
  padding-left: 150px !important;
}*/
.backbtn {
  width: 100px;
}
.headstyle {
  border-bottom: 2px solid #e5e5e5;
  padding-bottom: 20px;
}
.locsty {
  font-size: 16px;
  font-family: "Microsoft YaHei";
}
.iconsty {
  color: #53b9fe;
  font-size: 22px;
}
a {
  text-decoration: none;
  color: black;
}
.contentstyle {
  width: 75%;
  padding: 40px;
  padding-left: 100px;
}
.inputEdit {
  width: 48%;
}
.selectsty {
  width: 48%;
}
.btnsty {
  width: 85px;
}
</style>

